﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;

namespace is_it_messy
{
    public class MultiFileOp
    {
        //op copy==0 move==1 delete==2 encrypt==3 decrypt==4
        //targetPath 请不要加文件名
        public static Boolean Operate(int op, List<String> filePathsList, String targetPath = null)
        {
            foreach (string path in filePathsList)
            {
                switch (op)
                {
                    case 0:
                        return Copy(path, targetPath);
                    case 1:
                        return Move(path, targetPath);
                    case 2:
                        return Delete(path); 
                    case 3:
                        return Encrypt(path);
                    case 4:
                        return Decrypt(path);
                }
            }
            return false;
        }
        
        public static Boolean Rename(List<String> filePathsList, String name)
        {
            int count = 1;            
            if (filePathsList.Count == 1)
            {
                FileData data = new FileData(filePathsList[0]);
                String newFile = filePathsList[0].Replace(data.FileName,name);
                File.Move(data.AbsolutePath, newFile);
                return true;
            }
            int totalWidth = filePathsList.Count.ToString().Length;
            List<FileData> fileDatas = new List<FileData>();
            foreach (string path in filePathsList)
            {
                fileDatas.Add(new FileData(path));
            }
            SortByLastWriteDate(fileDatas);
            foreach (FileData data in fileDatas)
            {                
                String newFile = data.AbsolutePath.Replace(data.FileName,
                    name) + "_" + count.ToString().PadLeft(totalWidth, '0');
                File.Move(data.AbsolutePath, newFile);
                count++;
            }
            return true;
        }
        
        private static Boolean Copy(String filePath, String targetPath)
        {
            FileData file = new FileData(filePath);
            targetPath = targetPath + file.FileName;
            if (new FileInfo(targetPath).Exists)
            {
                return false;
            }
            File.Copy(filePath, targetPath, false);      
            return true;
        }

        private static Boolean Delete(String path)
        {
            if (new FileInfo(path).Exists)
            {
                File.Delete(path);
                return true;
            }
            return false;
        }

        private static Boolean Move(String filePath, String targetPath)
        {
            FileData file = new FileData(filePath);
            targetPath = targetPath + file.FileName;
            if (new FileInfo(targetPath).Exists)
            {
                return false;
            }
            File.Move(filePath, targetPath);      
            return true;
        }

        private static Boolean Encrypt(String path)
        {
            if (!new FileInfo(path).Exists)
            {
                File.Encrypt(path);
                return true;
            }
            return false;
        }

        private static Boolean Decrypt(String path)
        {
            if (!new FileInfo(path).Exists)
            {
                File.Decrypt(path);
                return true;
            }
            return false;
        }

        private static void SortByLastWriteDate(List<FileData> fileDatas)
        {
            QuickSort(fileDatas, 0, fileDatas.Count-1);
        }

        private static void QuickSort(List<FileData> fileDatas, int low, int high)
        {
            if (low >= high)  
            {  
                return;  
            }  
            int first = low, last = high;  
            //此时fileDatas[low]被保存到key，所以元素fileDatas[low]可以当作是一个空位，用于保存数据，之后每赋值一次，也会有一个位置空出来，直到last==first，此时fileDatas[last]==fileDatas[first]=key  
            DateTime key = fileDatas[low].LastWriteTime;
            FileData temp = fileDatas[low];
            while (first < last)  
            {  
                while (first < last && fileDatas[last].LastWriteTime >= key)  
                {  
                    last--;  
                }  
                fileDatas[first] = fileDatas[last];  
                while (first < last && fileDatas[first].LastWriteTime <= key)  
                {  
                    first++;  
                }  
                fileDatas[last] = fileDatas[first];  
            }  
            fileDatas[first] = temp;  
            //递归排序数组左边的元素  
            QuickSort(fileDatas, low, first - 1);  
            //递归排序右边的元素  
            QuickSort(fileDatas, first + 1, high);  
        }
    }
}